package misc;

/*******************************************************************************
 * Copyright (c) 2008, 2010 Xuggle Inc.  All rights reserved.
 *  
 * This file is part of Xuggle-Xuggler-Main.
 *
 * Xuggle-Xuggler-Main is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Xuggle-Xuggler-Main is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Xuggle-Xuggler-Main.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import gui.DrawImage;
import ij.ImagePlus;
import ij.process.ImageProcessor;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.imageio.ImageIO;
import javax.media.Buffer;
import javax.media.format.VideoFormat;
import javax.media.util.ImageToBuffer;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import camera.AbstractCamera;
import environment.AbstractEnvironment;
import environment.Environment1;
import environment.Environment2;
import tracking_algorithms.FrameProcessor;
import tracking_algorithms.Tracker;
import tracking_algorithms.blob.Blob;
import algos.Isovist;

/**
 * Takes a media container, finds the first video stream,
 * decodes that stream, and then displays the video frames,
 * at the frame-rate specified by the container, on a 
 * window.
 * @author aclarke
 *
 */
public class CameraPlayerTest implements ActionListener, ChangeListener
{
	private int timerDelay = 200;
    private DrawImage frame;
	private Timer timer ;
    private AbstractEnvironment env2;
    private FrameProcessor frameprocessor;
    ////////////////////*Test Variables*/////////////////////
  /**/  private final int NUMBER_OF_ELEMENTS = 1;          /**/
  /**/  Point[] examples = new Point[NUMBER_OF_ELEMENTS];  /**/
  /**/  private JButton up = new JButton("UP");            /**/
  /**/  private JButton down = new JButton("Down");        /**/
  /**/  private JButton left = new JButton("LEFT");        /**/
  /**/  private JButton right = new JButton("RIGHT");      /**/
  /**/	public BufferedImage background;                   /**/
  /**/ 	public BufferedImage input;                        /**/ 
  /**/	private JSlider max_BlobSize;					   /**/
  /**/  private int num = 0;                               /**/
	///////////////////////////////////////////////////////
    
	public CameraPlayerTest(){
		env2 = new Environment1();
//		frameprocessor = new FrameProcessor(env2,null,null);
		timer = new Timer(timerDelay, this);
		examples[0] = new Point(10,180);
		up.addActionListener(this);
		down.addActionListener(this);
		right.addActionListener(this);
		left.addActionListener(this);
		
//        JLabel max_Lable = new JLabel("maximum blob size", JLabel.CENTER);
//        max_Lable.setAlignmentX(Component.CENTER_ALIGNMENT);
//		max_BlobSize = new JSlider(JSlider.HORIZONTAL,
//                0, 1000, 50);
//		max_BlobSize.addChangeListener(this);
//        //Turn on labels at major tick marks.
//
//        max_BlobSize.setMajorTickSpacing(200);
//        max_BlobSize.setMinorTickSpacing(100);
//        max_BlobSize.setPaintTicks(true);
//        max_BlobSize.setPaintLabels(true);
//        max_BlobSize.setSize(100, 50);
//        max_BlobSize.setBorder(
//                BorderFactory.createEmptyBorder(0,0,100,0));
        Font font = new Font("Serif", Font.ITALIC, 15);
//        max_BlobSize.setFont(font);
		frame = new DrawImage(640,480);		
		frame.add(up);
		frame.add(down);
		frame.add(left);
		frame.add(right);
//		frame.add(max_BlobSize);
//		frame.add(max_Lable);
		frame.setVisible(true);
		timer.start();
	}
	
	
	public void start(BufferedImage javaImage){
		if(javaImage!=null){
			ImagePlus ips = new ImagePlus("",javaImage);
			ImageProcessor iprocessor = ips.getProcessor();
			iprocessor.findEdges();
        	removeNoise(iprocessor);
        	iprocessor.erode();
        	iprocessor.dilate();
        	iprocessor.dilate();
        	iprocessor.erode();
        	javaImage = iprocessor.getBufferedImage();
			if(num>30){
				ImageProcessor imp = new ImagePlus("", javaImage).getProcessor();
				for (int i = -5; i < 5; i++) {
					for (int j = -5; j < 5; j++) {
						for (int j2 = 0; j2 < examples.length; j2++) {
							try{
								imp.set(i+examples[j2].x, j+examples[j2].y, Integer.MAX_VALUE);
							}catch(Exception e){}
						}
					}
				}	  
				javaImage = imp.getBufferedImage();
			}


			frameprocessor.processFrame(javaImage,javaImage);
			frame.draw(javaImage);
			num++;
		}
	}

//			  for(Blob blob : blobs){
//				  g2.setColor(Color.red);
//				  int x = (int)blob.predectedX-blob.bounds.width/2;
//				  int y = (int)(vf.getSize().height- blob.predectedY)-blob.bounds.height/2 ;
//				  int width = blob.bounds.width;
//				  int height = blob.bounds.height;
//				  g2.drawRect(x, y, width, height);
//				  g2.setColor(Color.blue);
//				  g2.drawLine((int)blob.predectedX-5,(int)(vf.getSize().height- blob.predectedY),(int) blob.predectedX+5,(int)(vf.getSize().height- blob.predectedY));
//				  g2.drawString(blob.frameLabel+"", x+blob.bounds.width/2, y+blob.bounds.height/2);
//			  }


  /**
   * Forces the swing thread to terminate; I'm sure there is a right
   * way to do this in swing, but this works too.
   */
  private static void closeJavaWindow()
  {
    System.exit(0);
  }

  private void removeNoise(ImageProcessor javaImage) {
		// TODO Auto-generated method stub
		for (int i = 0; i < javaImage.getWidth(); i++) {
			for (int j = 0; j < javaImage.getHeight(); j++) {
				int c = javaImage.get(i, j);
				int bb =  (c & 0xff);
				int gg =  ((c >> 8) & 0xff);
				int rr =  ((c >> 16) & 0xff);
				if(bb<250&&gg<250&&rr<250)
					javaImage.set(i, j, 0);
			}
			
		}
	  
	}
  @Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
			if(e.getSource() == timer){
					start(input);
			}
			if(e.getSource()==up){
				int sign = 1;
				for (int i = 0; i< examples.length; i++) {
					examples[i].y+=5*sign;
					sign = -sign;
				}
			}
			if(e.getSource()==down){
				int sign = -1;
				for (int i = 0; i< examples.length; i++) {
					examples[i].y+=5*sign;
					sign = -sign;
				}
			}
			if(e.getSource()==right){
				int sign = +1;
				for (int i = 0; i< examples.length; i++) {
					examples[i].x+=5*sign;
					sign = -sign;
				}
			}
			if(e.getSource()==left){
				int sign = -1;
				for (int i = 0; i< examples.length; i++) {
					examples[i].x+=5*sign;
					sign = -sign;
				}
			}
  }	
  @Override
  public void stateChanged(ChangeEvent e) {
  	// TODO Auto-generated method stub
  	if(e.getSource()==max_BlobSize)
  		 System.out.println(((JSlider)e.getSource()).getValue());
  		
  }
  
  public static void main(String[] args) {
	  CameraPlayerTest cam = new CameraPlayerTest();
	  cam.background = new ImagePlus("background.png").getBufferedImage();
	  cam.input = new ImagePlus("input.png").getBufferedImage();
//	  cam.run();
}



}